﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Resources;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using BTM_Client_Application.Resources;

namespace BTM_Client_Application.SupportClasses
{
    public class Res
    {
        public static ResourceManager Rm = new ResourceManager(typeof(BTM_Client));
        public const string IpMask = "[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}";
        public const string MapIconName = "satelite_128";
        public const string DbCacheInFolderLocation = @"\TileDBv5\en\Data.gmdb";
        public static readonly SolidColorBrush ErrorBrush = new SolidColorBrush(Colors.DarkOrange){Opacity = 0.7};
        public static readonly SolidColorBrush OkBrush = new SolidColorBrush(Colors.Transparent);
        public static readonly SolidColorBrush OkMapMarkerBrush = new SolidColorBrush(Colors.GreenYellow) { Opacity = 0.7 };
        public static readonly SolidColorBrush HaveSeenErrorsBrush = new SolidColorBrush(Colors.LightBlue) { Opacity = 0.3 };
        public static readonly SolidColorBrush HaveSeenErrorsMapBrush = new SolidColorBrush(Colors.LightBlue) { Opacity = 0.7 };
        public static readonly SolidColorBrush AccentBlueBrush = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#41B1E1"));
        //
        public static readonly SolidColorBrush StartTimeFilterBrush = new SolidColorBrush(Colors.GreenYellow);
        public static readonly SolidColorBrush EndTimeFilterBrush = new SolidColorBrush(Colors.Orange);

        public static readonly SolidColorBrush ResetButtonActiveBrush = new SolidColorBrush(Colors.Gold);


        public static class StatusColor
        {
            public static readonly SolidColorBrush Critical = new SolidColorBrush(Colors.Red) {Opacity = 0.3};
            public static readonly SolidColorBrush Alarm = new SolidColorBrush(Colors.Orange) { Opacity = 0.3 };
            public static readonly SolidColorBrush Warning = new SolidColorBrush(Colors.Yellow) { Opacity = 0.3 };
            public static readonly SolidColorBrush Minor = new SolidColorBrush(Colors.LawnGreen) { Opacity = 0.3 };
            public static readonly SolidColorBrush Indeterminate = new SolidColorBrush(Colors.DeepSkyBlue) { Opacity = 0.3 };
            
        }

        public static void MakeOpacityAnimation(bool show, DependencyObject aniObject, TimeSpan length, bool ifScale = false, ScaleCenter scaleCenter = ScaleCenter.Center)
        {
            var myDoubleAnimation = new DoubleAnimation();
            DoubleAnimation myDoubleAnimation1;
            if (show)
            {
                //myDoubleAnimation1 = new DoubleAnimation(60, 0, new Duration(length));
                myDoubleAnimation1 = new DoubleAnimation(0.4, 1, new Duration(length));
                myDoubleAnimation.From = 0.0;
                myDoubleAnimation.To = 1.0;
            }
            else
            {
                //myDoubleAnimation1 = new DoubleAnimation(0, 60, new Duration(length));
                myDoubleAnimation1 = new DoubleAnimation(1, 0.4, new Duration(length));
                myDoubleAnimation.From = 1.0;
                myDoubleAnimation.To = 0.0;
            }

                myDoubleAnimation.Duration = new Duration(length);
                var myStoryboard = new Storyboard();
                myStoryboard.Children.Add(myDoubleAnimation);
                Storyboard.SetTarget(myDoubleAnimation, aniObject);
                Storyboard.SetTargetProperty(myDoubleAnimation, new PropertyPath(UIElement.OpacityProperty));

            if (ifScale)
            {
                var scale = new ScaleTransform(1.0, 1.0);
                switch (scaleCenter)
                {
                    case ScaleCenter.LeftTopCorner:
                        (aniObject as UIElement).RenderTransformOrigin = new Point(
                            0, 0);
                        break;
                    case ScaleCenter.RightTopCorner:
                        (aniObject as UIElement).RenderTransformOrigin = new Point(
                            1, 0);
                        break;
                    case ScaleCenter.RightBottomCorner:
                        (aniObject as UIElement).RenderTransformOrigin = new Point(
                            1, 1);
                        break;
                    case ScaleCenter.LeftBootomCorner:
                        (aniObject as UIElement).RenderTransformOrigin = new Point(
                            0, 1);
                        break;
                    case ScaleCenter.Center:
                        (aniObject as UIElement).RenderTransformOrigin = new Point(
                            0.5, 0.5);
                        break;
                    default:
                        throw new ArgumentOutOfRangeException("scaleCenter");
                }
                
                (aniObject as UIElement).RenderTransform = scale;
                scale.BeginAnimation(ScaleTransform.ScaleXProperty, myDoubleAnimation1);
                scale.BeginAnimation(ScaleTransform.ScaleYProperty, myDoubleAnimation1);
            }
            /*
            var myDoubleAnimation1 = new DoubleAnimation(0, 90, new Duration(length));
            SkewTransform skew = new SkewTransform();
            (aniObject as UIElement).RenderTransform = skew;
            skew.BeginAnimation(SkewTransform.AngleXProperty, myDoubleAnimation1);
            */
            var obj = aniObject as UIElement;
            if (obj != null)
            {
                obj.Visibility = Visibility.Visible;
            }
            myStoryboard.Begin();
            
        }

        public static void HideWithAnimation(DependencyObject aniObject, TimeSpan length, bool ifScale = false, ScaleCenter scaleCenter = ScaleCenter.Center)
        {
            MakeOpacityAnimation(false, aniObject, length, ifScale, scaleCenter);
            var t = new Thread(() =>
            {
                Thread.Sleep(length);
                (aniObject as UIElement).Dispatcher.Invoke(new Action(() =>
                {
                    var obj = aniObject as UIElement;
                    if (obj != null)
                    {
                        obj.Visibility = Visibility.Hidden;
                    }
                }));
            });
            t.Start();
        }

        public enum ScaleCenter
        {
            LeftTopCorner,
            RightTopCorner,
            RightBottomCorner,
            LeftBootomCorner,
            Center
        }

        public static bool CheckIp(string ipString)
        {
            IPAddress ip;
            return !(!IPAddress.TryParse(ipString, out ip) || !Regex.IsMatch(ipString, IpMask));
        }

        static public BitmapImage GetImage(string path)
        {
            var logo = new BitmapImage();
            logo.BeginInit();
            logo.UriSource = new Uri("../Images/" + path);
            logo.EndInit();
            return logo;
        }

        public static BitmapFrame ResizeImage(string imageName, int width, int height)
        {
            const int margin = 0;
            //var source = GetImageSource("satelite_128");
            var source = GetImageSource(imageName);

            var rect = new Rect(margin, margin, width - margin * 2, height - margin * 2);

            var group = new DrawingGroup();
            RenderOptions.SetBitmapScalingMode(group, BitmapScalingMode.HighQuality);
            group.Children.Add(new ImageDrawing(source, rect));

            var drawingVisual = new DrawingVisual();
            using (var drawingContext = drawingVisual.RenderOpen())
                drawingContext.DrawDrawing(group);

            var resizedImage = new RenderTargetBitmap(
                width, height,         // Resized dimensions
                96, 96,                // Default DPI values
                PixelFormats.Default); // Default pixel format
            resizedImage.Render(drawingVisual);

            return BitmapFrame.Create(resizedImage);
        }

        private static BitmapImage GetImageSource(string imageName)
        {
            var im = new BitmapImage();
            im.BeginInit();
            im.UriSource = new Uri("pack://application:,,,/BTM_Client_Application;component/Images/General/" + imageName + ".png");
            im.EndInit();
            return im;
        }
    }
}
